#ifndef FILESERVER_H
#define FILESERVER_H

#include <QObject>
#include <QFile>
#include  <QTimer>
#include <QPointer>
#include <QReadWriteLock>
#include <QWaitCondition>
class QWebSocketServer;
class QWebSocket;

class FileServer : public QObject
{
    Q_OBJECT
public:
    FileServer(QObject * parent = Q_NULLPTR);
    ~FileServer();

    void setFileName(const QString &value);

    void listen();
    quint16 port() const;
    QString token() const;

    qint64 size() const;
    void setSize(const qint64 &size);

signals:
    void closed();
    void progress(qint64 received, qint64 total);
private slots:
    void onNewConnection();
    void processTextMessage(const QString &message);
    void socketDisconnected();
    //void processBinaryMessage(const QByteArray &message);

    void processTimerCheckConnected();
    void notifyTransport();
    void notifyError(const QString &error);
private:
    void sendBinaryData();

private:
    QWebSocketServer *m_pWebSocketServer;
    QPointer<QWebSocket> m_client;
    QString m_fileName;
    QFile *m_file;
    QString m_token;

    int m_timerID;
    qint64 m_size;
    QReadWriteLock m_lock;
    QWaitCondition m_waitCondition;
    quint16 m_crc;
    qint64 m_offset;
    QByteArray m_buffer;
};

#endif // FILESERVER_H
